package com.intelligentmeterreadingsys.report.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.database.Cursor;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.rfid.RfidHelper;
import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.httpserver.RequestUrl;
import com.intelligentmeterreadingsys.httpserver.UploadHttpClient;
import com.intelligentmeterreadingsys.povo.EngineeringOrderPoVo;
import com.intelligentmeterreadingsys.povo.TypePoVo;
import com.intelligentmeterreadingsys.povo.WaterInfoVO;
import com.intelligentmeterreadingsys.report.db.ReportDao;
import com.intelligentmeterreadingsys.util.BluetoothHelp;
import com.intelligentmeterreadingsys.util.CHexConver;
import com.intelligentmeterreadingsys.util.FileNameUtil;
import com.intelligentmeterreadingsys.util.SafetyExitUtil;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.util.TipHelper;

import dbhelper.DbTableName;
import dbhelper.XmlNameHelper;

/**
 * 添加报案
 * 
 * @author king
 * 
 */
public class ReportAddActivity extends Activity implements OnClickListener {
	private static final int REQUEST_CODE_TAKE_VIDEO = 2;
	private static final int REQUEST_CODE_TAKE_IMAGE = 1;
	private static final int REQUEST_CODE_TAKE_SOUND = 3;

	public Button button1;// 保存

	private final static int DIALOG = 1;
	public Button button3;// 上传

	public EditText edit1;// 工程单名称

	public EditText edit2;// 工程单描述

	public TextView textview;// 水表编号

	public Spinner spinner;// 工程单类型

	public static final int ITEM0 = Menu.FIRST;// 系统值

	public String ismodify = "添加";

	public String videopath;

	public String soundpath;

	public String type;

	public int OrderID = 0;

	public ProgressDialog pd;

	public RfidHelper rh;// 读卡帮助对象

	public String xieyi = "15693";

	public int RFIDytpe = 2;

	public boolean isStop = true;

	public long time;

	public String waternumber = "";

	public Button button2;// 修改

	public boolean isupload = false;// 上传成功标志

	public boolean issave = false;

	public SafetyExitUtil safe = null;

	public ArrayList<String> imagepath = null;

	public BluetoothHelp bluehelp;

	public String readtype = "终端读卡";
	public String[] readdate;

	private NfcAdapter mAdapter;
	private PendingIntent mPendingIntent;
	private IntentFilter[] mFilters;
	private String[][] mTechLists;

	public Double latitude = 0.0;// 维度
	public Double longitude = 0.0;// 径度

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_report);
		init();
		bluehelp = new BluetoothHelp(this);
		bluehelp.init();
	}

	/**
	 * 初始化控件
	 */
	public void init() {
		button1 = (Button) findViewById(R.id.report_add_save);
		button2 = (Button) findViewById(R.id.report_add_modifys);
		button3 = (Button) findViewById(R.id.report_add_upload);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		edit1 = (EditText) findViewById(R.id.report_add_name);
		edit2 = (EditText) findViewById(R.id.report_add_des);
		spinner = (Spinner) findViewById(R.id.report_add_type);

		// 选择工程类型监听器
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				type = arg0.getItemAtPosition(arg2).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				type = "无";

			}
		});
		textview = (TextView) findViewById(R.id.report_add_waternumber);
		ReportDao dao = new ReportDao(this);

		List<TypePoVo> list = dao.queryOrderType();
		String date[] = null;
		if (list != null && list.size() > 0) {
			date = new String[list.size() + 1];
			date[0] = "请选择";
			for (int i = 0; i < list.size(); i++) {
				date[i + 1] = list.get(i).getName();
			}
		} else {
			date = new String[] { "请选择" };
		}

		dao.close();

		// 初始化下拉框数据
		ArrayAdapter<String> ada = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, date);// 放数据
		ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(ada);
		mAdapter = NfcAdapter.getDefaultAdapter(this);

		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
				getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
		IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
		try {
			ndef.addDataType("*/*");
			mFilters = new IntentFilter[] { ndef, };
			mTechLists = new String[][] {
					new String[] { NfcV.class.getName() },
					new String[] { MifareClassic.class.getName() } };

		} catch (MalformedMimeTypeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void onClick(View v) {

		if (v.getId() == button1.getId()) {
			if (waternumber == null || waternumber.equals("")) {
				Toast.makeText(ReportAddActivity.this, "没有水表!",
						Toast.LENGTH_LONG).show();
				return;
			}
			if (imagepath == null || imagepath.size() < 1) {
				Toast.makeText(ReportAddActivity.this, "还没拍照呢!",
						Toast.LENGTH_LONG).show();
				return;

			}
			showDialog(DIALOG + 1);
		} else if (v.getId() == button3.getId()) {
			if (OrderID < 1) {
				Toast.makeText(ReportAddActivity.this, "请保存后再上传!",
						Toast.LENGTH_LONG).show();
				return;
			}

			pd = new ProgressDialog(this);
			pd.setMessage("上传中.....");
			pd.show();
			new Thread() {

				@Override
				public void run() {
					ReportDao dao = new ReportDao(ReportAddActivity.this);
					List<EngineeringOrderPoVo> lists = dao.queryUpload(OrderID
							+ "");
					if (lists == null | lists.size() < 1) {
						Toast.makeText(ReportAddActivity.this, "上传失败", 1000)
								.show();
						dao.close();
						return;
					}
					EngineeringOrderPoVo e = dao.queryUpload(OrderID + "").get(
							0);
					dao.close();
					Map<String, String> map1 = new HashMap<String, String>();
					Map<String, String> map3 = new HashMap<String, String>();
					map3.put("desc", e.getDesc());
					map3.put("typeid", e.getTypenumber());
					map3.put("waternumber", e.getWaternumber());
					map3.put("name", e.getName());
					map3.put("latitude", e.getLatitude() + "");
					map3.put("longitude", e.getLongitude() + "");

					Map<File, String> map2 = new HashMap<File, String>();
					SharedPreferenceUtil sh = new SharedPreferenceUtil(
							ReportAddActivity.this);
					map3.put("imsi", sh.getValue(XmlNameHelper.IMSI));
					List<String> img = e.getImagepath();
					for (int i = 0; i < img.size(); i++) {
						if (img.get(i) != null) {
							File file = new File(img.get(i));
							if (file.exists()) {
								map1.put(file.getName(), "imagename");
								map2.put(file, "imagepath");
							}
						}
					}
					if (e.getSoundpath() != null) {
						File file = new File(e.getSoundpath());
						if (file.exists()) {
							map1.put(file.getName(), "soundname");
							map2.put(file, "soundpath");
						}
					}
					if (e.getVideopath() != null) {
						File file = new File(e.getVideopath());
						if (file.exists()) {
							map1.put(file.getName(), "videoname");
							map2.put(file, "videopath");
						}
					}
					RequestUrl r = new RequestUrl(ReportAddActivity.this);
					String url = r.getUrl(RequestUrl.UploadOrder);

					String respon = UploadHttpClient.uploadSubmit(url, map1,
							map2, map3);

					if (respon.equals("error")) {
						Message msg = myHandler.obtainMessage();
						msg.what = 1;
						myHandler.sendMessage(msg);
					} else {
						try {
							JSONObject json = new JSONObject(respon);
							String message = json.getString("message");
							if (message.equals("yes")) {
								Message msg = myHandler.obtainMessage();
								msg.what = 2;
								myHandler.sendMessage(msg);
							} else {
								Message msg = myHandler.obtainMessage();
								msg.what = 3;
								myHandler.sendMessage(msg);
							}
						} catch (JSONException e1) {
							Message msg = myHandler.obtainMessage();
							msg.what = 3;
							myHandler.sendMessage(msg);
						}

					}
				}

			}.start();

		} else if (v.getId() == button2.getId()) {
			if (OrderID < 1) {
				Toast.makeText(ReportAddActivity.this, "还没添加数据不能修改!",
						Toast.LENGTH_LONG).show();
				return;
			}
			if (isupload) {
				Toast.makeText(ReportAddActivity.this, "已经上传的数据不能修改!",
						Toast.LENGTH_LONG).show();
				return;
			}
			boolean b = modify();
			if (b) {
				Toast.makeText(ReportAddActivity.this, "修改成功!",
						Toast.LENGTH_LONG).show();
			}
		}

	}

	@Override
	public void finish() {

		if (rh != null) {
			isStop = false;
			rh.stopAutoRead();
			rh.close();
		}

		super.finish();
	}

	@Override
	protected void onDestroy() {
		if (rh != null) {
			isStop = false;
			rh.stopAutoRead();
			rh.close();
		}
		bluehelp.stop();

		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(0, ITEM0 + 1, 0, "拍照");
		menu.add(0, ITEM0 + 2, 0, "录视频");
		menu.add(0, ITEM0 + 3, 0, "录音");
		menu.add(0, ITEM0 + 4, 0, "读卡协议");
		menu.add(0, ITEM0 + 5, 0, "读卡方式");
		menu.findItem(ITEM0 + 1);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case ITEM0 + 1:

			Bundle bundle = new Bundle();

			bundle.putString("otherid", OrderID + "");
			bundle.putString("status", "1");
			bundle.putString("classname", "ReportAddActivity");
			Intent intent = new Intent(ReportAddActivity.this,
					OrderImageActivity.class);
			intent.putExtras(bundle);
			startActivityForResult(intent, REQUEST_CODE_TAKE_IMAGE);
			break;
		case ITEM0 + 2:
			videoMethod();
			break;
		case ITEM0 + 3:
			System.out.println("录音");
			soundRecorderMethod();
			break;
		case ITEM0 + 4:
			System.out.println("设置协议");
			isStop = true;

			showDialog(DIALOG);
			break;
		case ITEM0 + 5:
			showDialog(DIALOG + 2);
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 添加工程单
	 * 
	 * @return
	 */
	public boolean InsertOrder() {
		ReportDao dao = new ReportDao(this);
		ContentValues values = new ContentValues();
		values.put("ordername", edit1.getEditableText().toString());
		values.put("desc", edit2.getEditableText().toString());

		values.put("videopath", videopath);
		values.put("soundpath", soundpath);
		values.put("waternumber", waternumber);
		values.put("status", "0");
		values.put("ordertype", "0");
		values.put("latitude", latitude);
		values.put("longitude", longitude);
		int id = dao.insertReport(values, type);
		this.OrderID = id;

		boolean b = false;
		if (id > 0) {
			b = true;
		}
		if (OrderID > 0) {
			List<ContentValues> lists = new ArrayList<ContentValues>();
			for (int k = 0; k < imagepath.size(); k++) {
				ContentValues value = new ContentValues();
				value.put("otherid", OrderID);
				value.put("status", "1");
				value.put("type", "1");
				value.put("path", imagepath.get(k));

				lists.add(value);
			}
			b = dao.insertALL(lists, DbTableName.RESOURCES);
			dao.close();

		}
		return b;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public boolean modify() {
		boolean b = false;
		ReportDao dao = new ReportDao(this);
		ContentValues values = new ContentValues();
		values.put("ordername", edit1.getEditableText().toString());
		values.put("desc", edit2.getEditableText().toString());
		values.put("waternumber", waternumber);
		values.put("soundpath", soundpath);
		values.put("videopath", videopath);
		values.put("status", "0");
		values.put("latitude", latitude);
		values.put("longitude", longitude);
		b = dao.modifyOrder(values, OrderID + "", type);
		if (OrderID > 0) {
			List<ContentValues> lists = new ArrayList<ContentValues>();
			for (int k = 0; k < imagepath.size(); k++) {
				ContentValues value = new ContentValues();
				value.put("otherid", OrderID);
				value.put("status", "1");
				value.put("type", "1");
				value.put("path", imagepath.get(k));

				lists.add(value);
			}
			b = dao.insertALL(lists, DbTableName.RESOURCES);
		}
		dao.close();
		return b;
	}

	/**
	 * 删除工程单
	 * 
	 * @param id
	 * @return
	 */
	public boolean delectOrder(String id) {
		ReportDao dao = new ReportDao(this);
		boolean b = dao.delectOrder(id);

		dao.close();

		return b;
	}

	/**
	 * 读卡消息处理类
	 */
	public Handler MyHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			long times = System.currentTimeMillis();
			if (times - time > 12000) {
				Toast.makeText(ReportAddActivity.this, "读卡成功",
						Toast.LENGTH_SHORT).show();
				isStop = false;
				if (readtype.equals("手持终端")) {
					pd.dismiss();
				} else {
					bluehelp.close();
				}
				return;
			}
			if (msg.what == 1) {
				if (readtype.equals("手持终端")) {
					pd.dismiss();
				} else {
					bluehelp.close();
				}
				ReportDao dao = new ReportDao(ReportAddActivity.this);
				WaterInfoVO s = dao.queryWaterNumber(msg.obj.toString());

				dao.close();
				waternumber = s.getWaternumber();
				latitude = s.getLatitude();
				longitude = s.getLongitude();
				if (s == null || s.equals("")) {
					Toast.makeText(ReportAddActivity.this,
							"读卡成功" + msg.obj.toString() + ",此RFID没绑定水表", 1000)
							.show();
				} else {
					Toast.makeText(ReportAddActivity.this,
							"读卡成功" + msg.obj.toString(), 1000).show();
				}
				isupload = false;
				textview.setText(s.getWaternumber());
				TipHelper.Vibrate(ReportAddActivity.this, 1000);
			} else {
				isupload = false;
				Toast.makeText(ReportAddActivity.this, "读卡超时",
						Toast.LENGTH_LONG).show();
				waternumber = "";

			}
		}
	};

	// 执行回调函数
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE_TAKE_VIDEO) {
			System.out.println("返回视频");
			if (resultCode == RESULT_OK) {
				videopath = data.getData().toString().substring(7);
				System.out.println(videopath);
			}
		} else if (requestCode == REQUEST_CODE_TAKE_IMAGE) {
			if (resultCode == RESULT_OK) {
				imagepath = data.getStringArrayListExtra("imgpath");
			}
		} else if (requestCode == REQUEST_CODE_TAKE_SOUND) {
			if (resultCode == RESULT_OK) {
				Uri uriVideo = data.getData();
				Cursor cursor = this.getContentResolver().query(uriVideo, null,
						null, null, null);
				if (cursor.moveToNext()) {
					String strVideoPath = cursor.getString(cursor
							.getColumnIndex("_data"));
					FileInputStream fin = null;
					FileOutputStream fout = null;

					File file = new File(strVideoPath);

					try {
						fin = new FileInputStream(file);
						System.out.println(file.isFile());
						FileNameUtil f = new FileNameUtil(this);
						soundpath = f.soundpath();
						if (soundpath == null) {
							Toast.makeText(ReportAddActivity.this, "SD卡不可用",
									Toast.LENGTH_LONG).show();
							return;
						}
						String fileName = new SimpleDateFormat("yyyyMMddHHmmss")
								.format(new Date()) + ".amr";
						soundpath = soundpath + "/" + fileName;
						System.out.println(soundpath);
						fout = new FileOutputStream(soundpath);

						byte[] buffer = new byte[1024];
						int bytesRead;
						while ((bytesRead = fin.read(buffer)) > 0) {
							System.out.println("---");

							fout.write(buffer, 0, bytesRead);
						}
						fin.close();
						fout.close();
					} catch (FileNotFoundException e) {

						e.printStackTrace();
					} catch (IOException e) {

						e.printStackTrace();
					} finally {
						file.delete();
					}

				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	// 读卡
	Thread myThread = new Thread() {
		@Override
		public void run() {
			while (isStop) {
				String id = "";
				if (readtype.equals("终端读卡")) {
					id = rh.readIDString();

				} else {

					id = bluehelp.getRfidId();
				}
				if (!TextUtils.isEmpty(id)) {
					isStop = false;
					Message msg = MyHandler.obtainMessage();
					msg.what = 1;
					msg.obj = id;
					MyHandler.sendMessage(msg);
				}
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			super.run();
		}

	};

	/**
	 * 初始化读卡
	 * 
	 * @param type
	 */
	public void initRfid(int type) {
		// 初始化读帮助类
		rh = RfidHelper.getInstance();
		rh.setCardType(type);
		rh.open();
		rh.setAllNoOfTimes(1);
		rh.setTimeInterval(50);
		// 设置激活读卡器后等待的时间.sdk默认10
		rh.setPingTimeInterval(10);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG:
			Builder builder = new android.app.AlertDialog.Builder(this);
			// 设置对话框的图标
			builder.setIcon(R.drawable.buttondraw);
			// 设置对话框的标题
			builder.setTitle("单选按钮对话框");
			// 0: 默认第一个单选按钮被选中
			builder.setSingleChoiceItems(R.array.hobby, 0,
					new android.content.DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							xieyi = getResources()
									.getStringArray(R.array.hobby)[which];

						}
					});

			// 添加一个确定按钮
			builder.setPositiveButton(" 确 定 ",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							if (xieyi.equals("15693")) {
								initRfid(2);
								isStop = true;
							} else if (xieyi.equals("14443")) {
								initRfid(1);
								isStop = true;
							}
						}
					});
			// 创建一个单选按钮对话框
			dialog = builder.create();
			break;
		case DIALOG + 1:
			Builder builder1 = new android.app.AlertDialog.Builder(this);
			// 设置对话框的图标
			builder1.setIcon(R.drawable.buttondraw);
			builder1.setTitle("确定添加新的工程单？");
			builder1.setPositiveButton("确定",
					new android.content.DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							if (TextUtils.isEmpty(type)) {
								Toast.makeText(ReportAddActivity.this,
										"你还没选择类型！", Toast.LENGTH_LONG).show();
								return;
							}
							if (imagepath == null | imagepath.size() < 1) {
								Toast.makeText(ReportAddActivity.this,
										"你还没拍照呢", Toast.LENGTH_LONG).show();
								return;
							}
							boolean b = InsertOrder();
							if (b) {
								Toast.makeText(ReportAddActivity.this,
										"添加数据成功", Toast.LENGTH_LONG).show();
								System.out.println("添加数据成功");
								issave = true;
							} else {
								issave = false;
							}

						}
					});
			builder1.setNeutralButton("取消",
					new android.content.DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

						}
					});
			// 创建一个单选按钮对话框
			dialog = builder1.create();
			break;
		case DIALOG + 2:
			if (readtype.equals("终端读卡")) {
				readdate = new String[] { "终端读卡", "蓝牙读卡" };
			} else {
				readdate = new String[] { "蓝牙读卡", "终端读卡" };
			}

			builder = new android.app.AlertDialog.Builder(this);
			// 设置对话框的图标
			builder.setIcon(R.drawable.buttondraw);
			// 设置对话框的标题
			builder.setTitle("读卡方式选择");
			// 0: 默认第一个单选按钮被选中

			builder.setSingleChoiceItems(readdate, 0,
					new android.content.DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							readtype = readdate[which];

						}
					});

			// 添加一个确定按钮
			builder.setPositiveButton(" 确 定 ",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							time = System.currentTimeMillis();
							if (readtype.equals("蓝牙读卡")) {
								bluehelp.start();
							}
							isStop = true;

						}
					});
			// 创建一个单选按钮对话框
			dialog = builder.create();
			break;

		}
		return dialog;
	}

	// 拍摄视频
	private void videoMethod() {
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		FileNameUtil f = new FileNameUtil(this);
		videopath = f.videopath();
		String fileName = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date()) + ".avi";
		File out = new File(videopath);
		out = new File(videopath, fileName);
		Uri uri = Uri.fromFile(out);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
	}

	/**
	 * 发送短息
	 */
	public void sendMSM(String number, String name) {
		Uri smsToUri = Uri.parse("smsto:" + number);
		Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);

		sendIntent.putExtra("sms_body", "尊敬的" + name + "用户:你的水表已绑定RFID卡");

		startActivity(sendIntent);
		System.out.println("发短息");
	}

	// 录音功能
	private void soundRecorderMethod() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("audio/*");

		startActivityForResult(intent, REQUEST_CODE_TAKE_SOUND);
	}

	/**
	 * 上传更新消息
	 */
	Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();

			if (msg.what == 2) {
				isupload = true;
				Toast.makeText(ReportAddActivity.this, "上传成功",
						Toast.LENGTH_LONG).show();
				ReportDao dao = new ReportDao(ReportAddActivity.this);
				dao.delectOrder(OrderID + "");
				boolean b = dao.delectImg(OrderID + "");
				dao.close();
				if (b) {
					OrderID = 0;
				}

			} else {
				Toast.makeText(ReportAddActivity.this, "上传失败",
						Toast.LENGTH_LONG).show();
			}
		}

	};

	@Override
	protected void onPause() {

		mAdapter.disableForegroundDispatch(this);

		super.onPause();
	}

	@Override
	protected void onResume() {
		mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
				mTechLists);

		super.onResume();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		// if (isStop) {
		// return;
		// }
		Toast.makeText(this, intent.getAction(), 1000).show();
		System.out.println(intent.getAction());
		if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
			Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
			for (String tech : tagFromIntent.getTechList()) {
				System.out.println(tech);
			}
			time = System.currentTimeMillis();
			MifareClassic mf = MifareClassic.get(tagFromIntent);
			System.out.println(mf == null);
			if (mf != null) {

				String s = CHexConver.bytesToHexStrings(mf.getTag().getId());
				Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
			}
			NfcV nf = NfcV.get(tagFromIntent);
			if (nf != null) {
				String s = CHexConver.bytesToHexStrings(nf.getTag().getId());
				Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
			}

		}

		super.onNewIntent(intent);
	}
}
